#!/usr/bin/env python3.10.6
# -*- coding: utf-8 -*-
# Author: Bobby Sheng <Bobby@sky-cloud.net>

from jira import JIRA


class jira_get:
    def __init__(self, jira):
        self.jira = jira
        pass

    def get_projects(self):
        """
            [jira所有项目名称]
            :param none
            :return: list
        """
        projects = self.jira.projects()
        print(projects)
        return projects

    def get_issue(self, issue_id):
        """
            [获取issue信息]
            :param 'JRA-1330'
            :return: list
        """
        issue = self.jira.issue(issue_id)
        return issue

    def add_watcher_issue(self, issue, username):
        """
            [issue添加关注者]
            :param issue 例如：'JRA-1330'
            :param username 例如：'liyc'
            :return: list
        """
        self.jira.add_watcher(issue, username)

    def get_watcher_issue(self, issue):
        """
            [获取issue所有关注者]
            :param issue 例如：'JRA-1330'
            :return: list
        """
        print(self.jira.watchers(issue))
        return self.jira.watchers(issue)

    def get_comment_all_issue(self, issue):
        """
            [获取issue所有评论]
            :param issue 例如：'JRA-1330'
            :return: list
        """
        print(self.jira.comments(issue))
        return self.jira.comments(issue)

    def get_comment_issue(self, issue, comment_id):
        """
            [issue获取某条评论]
            :param issue 例如：'JRA-1330'
            :param comment_id 例如：'10234'
            :return: list
        """
        return self.jira.comment(issue, comment_id)

    def add_comment_issue(self, issue):
        """
            [issue新增评论]
            :param issue 例如：'JRA-1330'
            :param comment_id 例如：'10234'
            :return: list
        """
        self.jira.add_comment(issue, 'new comment')

    def update_comment_issue(self, issue, comment_id, text):
        """
            [issu更新评论]
            :param issue 例如：'JRA-1330'
            :param comment_id 例如：'10234'
            :return: list
        """
        comment = self.jira.comment(issue, comment_id)
        return comment.update(body=text)  # 更新评论

    def delete_comment_issue(self, issue, comment_id):  # comment_id:'10234'
        """
            [issue删除评论]
            :param issue 例如：'JRA-1330'
            :param comment_id 例如：'10234'
            :return: list
        """
        comment = self.jira.comment(issue, comment_id)  # 某条评论
        comment.delete()  # 删除该评论

    def get_attachment_issue(self, issue):
        """
            [获取issue附件]
            :param issue 例如：'JRA-1330'
            :return: list
        """
        print(issue.fields.attachment)  # 问题附件
        return issue.fields.attachment

    def add_attachment_issue(self, issue, attachment):
        """
            [issue添加附件]
            :param issue 例如：'JRA-1330'
            :param attachment 例如：'/some/path/attachment.txt'
            :return: list
        """
        self.jira.add_attachment(
            issue, attachment=attachment)  # 添加附件

    def add_issue(self, issue_dict):
        """
            创建issue
            :param issue_dict 例如：issue_dict = {
                                                'project': {'id': 123},
                                                'summary': 'New issue from jira-python',
                                                'description': 'Look into this one',
                                                'issuetype': {'name': 'Bug'},
                                            }
            :return: none
        """
        self.jira.create_issue(fields=issue_dict)

    def add_issues(self, issue_list):
        """
            [批量创建issue]
            :param issue_dict 例如：issue_list = [
                                                {
                                                    'project': {'id': 123},
                                                    'summary': 'First issue of many',
                                                    'description': 'Look into this one',
                                                    'issuetype': {'name': 'Bug'},
                                                },
                                                {
                                                    'project': {'key': 'FOO'},
                                                    'summary': 'Second issue',
                                                    'description': 'Another one',
                                                    'issuetype': {'name': 'Bug'},
                                                },
                                                {
                                                    'project': {'name': 'Bar'},
                                                    'summary': 'Last issue',
                                                    'description': 'Final issue of batch.',
                                                    'issuetype': {'name': 'Bug'},
                                                }]
            :return: none
        """
        self.jira.create_issues(field_list=issue_list)

    def assign_issues(self, issue, newassignee):
        # 分配问题
        self.jira.assign_issue(issue, newassignee)

    # 转换问题
    # jira.transition_issue(issue, '5', assignee={'name': 'pm_user'}, resolution={'id': '3'})

    def search_issue(self, jql):
        """
            [搜索issue]
            Jira的搜索非常强大，并配有一套专门的搜索语言，称为JQL(Jira Query Language)，Jira的Python库便是基于JQL语法进行搜索的，返回的是搜索到的问题列表。
            默认最大结果数未1000，可以通过maxResults参数配置，该参数为-1时不限制数量，返回所有搜索结果。
            :param jql jirajql语句
            :return list
        """
        return self.jira.search_issues(jql, maxResults=-1)


'''
        [创建/分配/转换问题]
        jira.create_issue(): 创建问题
        jira.create_issues(): 批量创建问题
        jira.assign_issue(): 分配问题
        jira.transitions(): 获取问题的工作流
        jira.transition_issue(): 转换问题
        [关注者/评论/附件]
        jira.watchers(): 问题的关注者
        jira.add_watcher(): 添加关注者
        jira.remove_watcher(): 移除关注者
        jira.comments(): 问题的所有评论
        jira.comment(): 某条评论
        jira.add_comment()：添加评论
        comment.update()/delete(): 更新/删除评论
        jira.add_attachment(): 添加附件

'''
